<template>
  <a-card :bordered="false" title="检查单识别模板">
    <a-form layout="inline">
      <a-row :gutter="24">
        <a-col :md="6" :sm="12">
          <a-form-item label="上传检查单模板">
            <a-upload
              name="file"
              :data="{'imgType': 2}"
              :multiple="false"
              :action="uploadAction"
              @change="handleChange">
              <template>
                <a-button> <a-icon type="upload" />上传 </a-button>
              </template>
            </a-upload>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row style="margin-top: 15px">
        <a-table
          v-if="dataSource.length > 0"
          :loading="loading"
          key="title"
          :columns="columns"
          :pagination="false"
          :data-source="dataSource"
        >
          <span slot="action"  slot-scope="text, record, index">
            <a href="javascript:" @click="handleMapping(record, index)">数据映射</a>
          </span>
        </a-table>
      </a-row>
    </a-form>
    <mapping-modal ref="modalForm" @ok="modalFormOk"></mapping-modal>
  </a-card>
</template>

<script>
import { getAction, postAction } from '@/api/manage'
import moment from 'moment'
import { mapGetters } from 'vuex'
import MappingModal from './modules/MappingModal'


export default {
  name: 'OCRIndex',
  components: { MappingModal },
  data() {
    return {
      loading: true,
      items: [],
      fileList: [],
      uploadAction: '/ocrapi/ocr/upload',
      columns: [
        {
          title: '检查项',
          dataIndex: 'word',
          align: 'center'
        },
        {
          title: '映射字段',
          dataIndex: 'mappingText',
          align: 'center'
        },
        {
          title: '操作',
          key: 'operation',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      dataSource: [],
      testData: '{"words_result":{"CommonData":[{"word_name":"医院","word":"暨南大学附属第一医院东圃院区"},{"word_name":"报告单名称","word":"检验报告单"},{"word_name":"姓名","word":"李镇权"},{"word_name":"科室","word":"东圃普外科门诊"},{"word_name":"性别","word":"男年龄:3"},{"word_name":"标本种类","word":"血清"},{"word_name":"时间","word":"2022829"},{"word_name":"标本情况","word":""},{"word_name":"临床症状","word":""},{"word_name":"临床诊断","word":""},{"word_name":"建议","word":""},{"word_name":"年龄","word":""},{"word_name":"检查结果","word":""},{"word_name":"检查目的","word":""},{"word_name":"检查项目","word":""}],"Item":[[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":""},{"word_name":"参考区间","word":""},{"word_name":"测试方法","word":""},{"word_name":"结果","word":"1.96"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"ALT/AST"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"umol/L"},{"word_name":"参考区间","word":"210-420"},{"word_name":"测试方法","word":"终点法"},{"word_name":"结果","word":"445"},{"word_name":"结果提示","word":"|"},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"尿酸(URIC)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"mmol/L"},{"word_name":"参考区间","word":"2.9--8.3"},{"word_name":"测试方法","word":"速率法"},{"word_name":"结果","word":"7.0"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"尿素"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":""},{"word_name":"参考区间","word":""},{"word_name":"测试方法","word":""},{"word_name":"结果","word":"0.13"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"UREL/CR比值"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"mmol/L"},{"word_name":"参考区间","word":"空腹3.6--6.1"},{"word_name":"测试方法","word":"终点法"},{"word_name":"结果","word":"5.30"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"葡萄糖(GLU)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"mmol/L"},{"word_name":"参考区间","word":"0.48--1.71"},{"word_name":"测试方法","word":"终点法"},{"word_name":"结果","word":"1.15"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"甘油三酯(TG)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"mmol/L"},{"word_name":"参考区间","word":"3.1--5.7"},{"word_name":"测试方法","word":"终点法"},{"word_name":"结果","word":"5.1"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"总胆固醇"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"U/L"},{"word_name":"参考区间","word":"5--40"},{"word_name":"测试方法","word":"酶动力学法"},{"word_name":"结果","word":"49.00"},{"word_name":"结果提示","word":"↑"},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"丙氨酸氨基转换酶(ALT)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"U/L"},{"word_name":"参考区间","word":"8--40"},{"word_name":"测试方法","word":"酶动力学法"},{"word_name":"结果","word":"25.00"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"门冬氨酸氨基转换酶(AST)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"U/L"},{"word_name":"参考区间","word":"11--61"},{"word_name":"测试方法","word":"酶动力学法"},{"word_name":"结果","word":"41"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"Y_谷氨酰转肽酶(γ_GT)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"U/L"},{"word_name":"参考区间","word":"12--40"},{"word_name":"测试方法","word":"酶动力学法"},{"word_name":"结果","word":"28.59"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"a岩糖藻苷酶"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"U/L"},{"word_name":"参考区间","word":"53--140"},{"word_name":"测试方法","word":"酶动力学法"},{"word_name":"结果","word":"63"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"碱性磷酸酶(ALP)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"umol/L"},{"word_name":"参考区间","word":"30--104"},{"word_name":"测试方法","word":"固定时间法"},{"word_name":"结果","word":"53"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"肌酐(CREA)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"mmol/L"},{"word_name":"参考区间","word":"0.91--2.05"},{"word_name":"测试方法","word":"终点法"},{"word_name":"结果","word":"1.21"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"高密度脂蛋白(HDL_c)"}],[{"word_name":"仪器类型","word":""},{"word_name":"单位","word":"mmol/L"},{"word_name":"参考区间","word":"1.94--3.61"},{"word_name":"测试方法","word":"终点法"},{"word_name":"结果","word":"3"},{"word_name":"结果提示","word":""},{"word_name":"项目代号","word":""},{"word_name":"项目名称","word":"低密度脂蛋白C(LDLc)"}]]},"Item_row_num":15,"CommonData_result_num":15,"log_id":1566985596368908804}'
    }
  },
  computed: {
    
  },
  created() {
    this.handleResult(this.testData)
  },
  methods: {
    ...mapGetters(['doctorId', 'orgCode']),
    handleChange(info) {
      if(info.file.status==='done'){
        if(info.file.response.returnCode == 10000){
          this.handleResult(info.file.response.data)
          this.$message.success(`${info.file.name} 识别成功!`);
        }
        //this.$message.success(`${info.file.name} 上传成功!`);
      }
    },
    handleResult(data) {
      console.log(typeof data)
      if (typeof data === 'string') {
        data = JSON.parse(data)
      }
      let dataSource = data?.words_result?.Item || []
      dataSource = this.handleFormatData(dataSource)
      this.loading = true
      postAction('/ocr/mapping/search', {
        words: dataSource
      }).then(res => {
        if (res.success) {
          this.dataSource = res.result
        }
      }).finally(_ => {
        this.loading = false
      })

    },
    handleFormatData(data) {
      let newData = []
      if(data.length > 0) {
        data.forEach(item => {
          item.forEach(row => {
            if(row.word_name == '项目名称') {
              newData.push(row.word)
            }
          })
        })
      }
      return newData
    },
    handleMapping(record, index) {
      this.$refs.modalForm.edit(record, index);
    },
    modalFormOk(data) {
      if(data.index != -1) {
        this.dataSource[data.index].mappingText = data.mappingText
      }
    }
  }
}
</script>

<style lang="less" scoped></style>

